using UnityEngine;

namespace Coffee.UIExtensions
{
    // [Assembly-CSharp.dll].Coffee.UIExtensions.ParameterTexture
    [Serializable]
    public class ParameterTexture
    {
        
        #region Coffee.UIExtensions.ParameterTexture.<>c
        // [Assembly-CSharp.dll].Coffee.UIExtensions.ParameterTexture.<>c
        [Serializable]
        private sealed class __c
        {
            // Fields
            public static readonly Coffee.UIExtensions.ParameterTexture.<>c __9;
            public static UnityEngine.Canvas.WillRenderCanvases __9__16_0;
            
            // Methods
            private static __c()
            {
                object val_1 = 536899569;
                val_1 = new System.Object();
                mem2[0] = val_1;
                throw new NullReferenceException();
            }
            public __c()
            {
            
            }
            internal void <Initialize>b__16_0()
            {
                if((mem[536890727] + 24) < 1)
                {
                        return;
                }
                
                var val_2 = 0;
                do
                {
                    Unknown val_1 = mem[536890727].Item[0];
                    val_2 = val_2 + 1;
                    if((mem[536890727] + 24) == val_2)
                {
                        return;
                }
                
                }
                while(mem[536890727] != 0);
                
                throw new NullReferenceException();
            }
        
        }
        
        #endregion
        
        // Fields
        private UnityEngine.Texture2D _texture;
        private bool _needUpload;
        private int _propertyId;
        private readonly string _propertyName;
        private readonly int _channels;
        private readonly int _instanceLimit;
        private readonly byte[] _data;
        private readonly System.Collections.Generic.Stack<int> _stack;
        private static System.Collections.Generic.List<System.Action> updates;
        
        // Methods
        public ParameterTexture(int channels, int instanceLimit, string propertyName)
        {
            string val_9 = val_1;
            val_1 = new System.Object();
            this._propertyName = val_9;
            int val_2 = channels - 1;
            int val_4 = instanceLimit - 1;
            int val_5 = (val_2 < 0) ? (channels + 2) : (val_2);
            int val_6 = (val_4 < 0) ? instanceLimit : (val_4);
            val_5 = val_5 & 4294967292;
            val_6 = val_6 & 4294967294;
            val_5 = val_5 + 4;
            val_6 = val_6 + 2;
            this._channels = val_5;
            this._instanceLimit = val_6;
            var val_7 = val_6 * val_5;
            this._data = 536880965;
            System.Collections.Generic.Stack<System.Int32> val_8 = 536904021;
            val_8 = new System.Collections.Generic.Stack<System.Int32>(capacity:  this._instanceLimit);
            this._stack = val_8;
            int val_10 = this._instanceLimit;
            val_10 = val_10 + 1;
            if(val_10 < 2)
            {
                    return;
            }
            
            val_9 = 27400392;
            do
            {
                this._stack.Push(item:  1);
                int val_11 = this._instanceLimit;
                val_11 = val_11 + 1;
            }
            while((1 + 1) < val_11);
        
        }
        public void Register(Coffee.UIExtensions.IParameterTexture target)
        {
            var val_4;
            var val_5;
            var val_6;
            this.Initialize();
            val_4 = target;
            val_5 = 0;
            if(target > 0)
            {
                    return;
            }
            
            if(null < 1)
            {
                    return;
            }
            
            Unknown val_2 = this._stack.Pop();
            val_5 = 1;
            val_6 = target;
            goto val_2;
        }
        public void Unregister(Coffee.UIExtensions.IParameterTexture target)
        {
            var val_4;
            var val_5;
            var val_6;
            var val_7;
            val_4 = target;
            if(target < 1)
            {
                    return;
            }
            
            val_5 = target;
            val_6 = -1073697961;
            this._stack.Push(item:  target);
            val_6 = 1;
            val_7 = target;
            throw new NullReferenceException();
        }
        public void SetData(Coffee.UIExtensions.IParameterTexture target, int channelId, byte value)
        {
            var val_4;
            var val_5;
            val_4 = target;
            val_5 = target;
            if(target < 1)
            {
                    return;
            }
            
            Coffee.UIExtensions.IParameterTexture val_3 = target - 1;
            val_3 = channelId + (this._channels * val_3);
            if(val_3 < this._data.Length)
            {
                    if(this._data[val_3] == value)
            {
                    return;
            }
            
                mem2[0] = value;
                this._needUpload = true;
                return;
            }
        
        }
        public void SetData(Coffee.UIExtensions.IParameterTexture target, int channelId, float value)
        {
            value = value * 255f;
            float val_1 = (value >= 0) ? (value) : 0f;
            this.SetData(target:  target, channelId:  channelId, value:  (val_1 < 0) ? ((int)val_1) : ((int)val_1));
        }
        public void RegisterMaterial(UnityEngine.Material mat)
        {
            if(this._propertyId == 0)
            {
                    this._propertyId = UnityEngine.Shader.PropertyToID(name:  this._propertyName);
            }
            
            if((UnityEngine.Object.op_Implicit(exists:  mat)) == false)
            {
                    return;
            }
            
            mat.SetTexture(nameID:  this._propertyId, value:  this._texture);
        }
        public float GetNormalizedIndex(Coffee.UIExtensions.IParameterTexture target)
        {
            Coffee.UIExtensions.IParameterTexture val_2 = target;
            float val_2 = (float)target;
            val_2 = val_2 + (-0.5f);
            float val_3 = (float)this._instanceLimit;
            val_3 = val_2 / val_3;
            return (float)val_3;
        }
        private void Initialize()
        {
            int val_13;
            var val_14;
            UnityEngine.Canvas.UnityEngine.Canvas.WillRenderCanvases val_15;
            var val_16;
            if(mem[536890727] == 0)
            {
                    System.Collections.Generic.List<System.Object> val_1 = 536903657;
                val_13 = 27363344;
                val_1 = new System.Collections.Generic.List<System.Object>();
                mem2[0] = val_1;
                val_14 = 536899569;
                if(mem[536899793] == 0)
            {
                    val_14 = 536899569;
            }
            
                val_15 = mem[mem[536899753] + 8];
                val_15 = mem[536899753] + 8;
                if(val_15 == 0)
            {
                    if(mem[536899793] == 0)
            {
                    val_14 = 536899569;
            }
            
                UnityEngine.Canvas.UnityEngine.Canvas.WillRenderCanvases val_2 = 536897777;
                val_15 = val_2;
                val_2 = new UnityEngine.Canvas.UnityEngine.Canvas.WillRenderCanvases(object:  mem[536899753], method:  1610678815);
                mem2[0] = val_15;
            }
            
                UnityEngine.Canvas.add_willRenderCanvases(value:  val_15);
            }
            
            val_16 = this;
            if((UnityEngine.Object.op_Implicit(exists:  this._texture)) == true)
            {
                    return;
            }
            
            val_13 = this._instanceLimit;
            UnityEngine.Texture2D val_9 = 536894117;
            val_9 = new UnityEngine.Texture2D(width:  ((this._channels < 0) ? (this._channels + 3) : this._channels) >> 2, height:  val_13, textureFormat:  4, mipChain:  false, linear:  (UnityEngine.QualitySettings.activeColorSpace == 1) ? 1 : 0);
            this._texture = val_9;
            this._texture.filterMode = 0;
            this._texture.wrapMode = 1;
            System.Action val_10 = 536881997;
            val_16 = mem[mem[536890727]];
            val_16 = mem[536890727];
            val_10 = new System.Action(object:  this, method:  1610678809);
            var val_13 = mem[536890727] + 28;
            val_13 = val_13 + 1;
            mem2[0] = val_13;
            if((mem[536890727] + 24) < (mem[536890727] + 16 + 24))
            {
                    var val_12 = (mem[536890727] + 16) + ((mem[536890727] + 24) << 3);
                mem2[0] = (mem[536890727] + 24) + 1;
                mem2[0] = val_10;
            }
            
            this._needUpload = true;
        }
        private void UpdateParameterTexture()
        {
            if(this._needUpload == false)
            {
                    return;
            }
            
            if((UnityEngine.Object.op_Implicit(exists:  this._texture)) == false)
            {
                    return;
            }
            
            this._needUpload = false;
            this._texture.LoadRawTextureData(data:  this._data);
            this._texture.Apply(updateMipmaps:  false, makeNoLongerReadable:  false);
        }
    
    }

}
